package com.example.qianjun.robotbetago;

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


    //指令集合，1~9表示移动步数，10表示右转，11左转，12后转
    int[] order = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    //自动执行的指令集合
    int[] orderRun = new int[]{1, 10, 2, 11, 2, 3, 3, 11, 1, 2, 12, 1, 11, 1, 11, 1, 1, 12, 10, 3, 11, 4, 11, 5, 10, 2, 10, 9, 10, 10, 2, 12, 1, 10, 3, 10, 1, 10, 1, 10, 1, 11, 1, 11, 4};

    RobotPanel robotPanel;
    EditText editText;

    boolean resetFlag =true;

    boolean autoRunflag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GlobalUtil.getTextSize(this);

        CheckBoard.init();
        setContentView(R.layout.activity_main);
        robotPanel = (RobotPanel) findViewById(R.id.robotpanel);
        editText = (EditText) findViewById(R.id.inputsteps);

        init();
    }

    public void init() {
        Robot.setPoint(new Point(3, 2));
        Robot.setDirect(0);
        CheckBoard.init();

    }

    public void moveSteps(View view) {
        int steps = Integer.parseInt(editText.getText().toString());
        Robot.goForSteps(steps);
        //robotPanel.invalidate();
        robotPanel.postInvalidate();


    }

    public void turnRight(View view) {
        Robot.turnRignt();
        robotPanel.postInvalidate();
    }

    public void turnLeftDir(View view) {
        Robot.turnLeft();
        robotPanel.postInvalidate();
    }

    public void turnBack(View view) {
        Robot.turnBack();
        robotPanel.postInvalidate();
    }

    public void autoRunSteps(View view) {
        if(autoRunflag){
            autoRunflag = false;
            resetFlag = false;
            new Thread() {
                @Override
                public void run() {
                    synchronized (this){
                        for (int i = 0; i < orderRun.length; i++) {
                            runOrder(orderRun[i]);
                            if (orderRun[i] <= 9) {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    Thread.sleep(300);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        resetFlag = true;
                    }


                }
            }.start();
        }


    }

    //根据指令执行具体操作
    public void runOrder(int order) {
        if (order <= 9) {
            Robot.goForSteps(order);
        } else if (order == 10) {
            Robot.turnRignt();
        } else if (order == 11) {
            Robot.turnLeft();
        } else if (order == 12) {
            Robot.turnBack();
        }

        robotPanel.postInvalidate();
    }

    public void resetDesktop(View view) {

        if(resetFlag){
            autoRunflag = true;
            init();
            Robot.setDirChar('→');
            robotPanel.postInvalidate();
        }

    }
}
